﻿using System;
using System.Data;
using System.Web.UI.WebControls;
using mojoPortal.Web;
using Resources;
using mojoPortal.Web.Framework;
using System.Collections;
using mojoPortal.Business;
using Telerik.Web.UI;
using ud_gSDMS.Code_Service;
using System.Linq;
using System.Web.UI;
using System.IO;

namespace ud_gSDMS.testing
{
    public partial class Report : mojoBasePage
    {
        private int pageId = -1;
        private int moduleId = -1;
        private SiteUser _siteUser;

        void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (SiteUtils.GetCurrentSiteUser()==null)
                {
                    WebUtils.SetupRedirect(this, SiteRoot + "/AccessDenied.aspx");
                }
                else
                {
                    _siteUser = SiteUtils.GetCurrentSiteUser();

                    if (!Page.IsPostBack)
                    {
                        PopulateLabels();
                        PopulateControls();
                    }
                }
            }
            catch
            {
            }
        }
        private void PopulateLabels()
        {

        }
        private void PopulateControls()
        {
            datFrom_Date.SelectedDate = new DateTime(2000,1,1);
            datTo_Date.SelectedDate = DateTime.Now;

            LoadControl();
            LoadGrid();

            datFrom_Date.SelectedDate = DateTime.Now.AddMonths(-1);
        }

        private void LoadGrid()
        {

                var data = gSDMS_BL.ud_gSDMS_PROJECT_TEST_Search(cboProject_Code.SelectedValue,
                                                                    cboProject_Name.SelectedValue,
                                                                    datFrom_Date.SelectedDate.Value.ToString(),
                                                                    datTo_Date.SelectedDate.Value.ToString(),
                                                                    cboSender.SelectedValue,
                                                                    cboAssigned_To.SelectedValue, cboStatus.SelectedValue);
                if (ViewState["SortExpression"] != null)
                {
                    var dv = data.DefaultView;
                    dv.Sort = ViewState["SortExpression"].ToString();
                    data = dv.Table;
                }

                grdProject_Test_List.DataSource = data;
                grdProject_Test_List.DataBind();

                ViewState["listProjectTest"] = data;

                if (grdProject_Test_List.Rows.Count > 0)
                    btnExport_Excel.Visible = true;
        }

        private void LoadControl()
        {

            var status = gSDMS_BL.ud_gSDMS_CAT_PROJECT_TEST_STATUS_Lst();
            cboStatus.Items.Add(new RadComboBoxItem("", ""));
            foreach (var p in status)
            {
                cboStatus.Items.Add(new RadComboBoxItem(p.Name, p.CAT_Project_Test_Status_ID.ToString()));
                if (p.Name == "Open")
                    cboStatus.SelectedIndex = cboStatus.Items.Count - 1;
            }

                var projects = gSDMS_BL.ud_gSDMS_PROJECT_TEST_LstArray();

                cboProject_Code.Items.Add(new RadComboBoxItem("", ""));
                foreach (var p in projects)
                {
                    cboProject_Code.Items.Add ( new RadComboBoxItem( p.ud_gSDMS_PROJECT.Code, p.Project_ID.ToString()));
                }
                cboProject_Code.SelectedValue = "";

                cboProject_Name.Items.Add(new RadComboBoxItem("", ""));
                foreach (var p in projects)
                {
                    cboProject_Name.Items.Add(new RadComboBoxItem(p.ud_gSDMS_PROJECT.Name, p.Project_ID.ToString()));
                }


                var sender = gSDMS_BL.mp_User_Lst("13");
                cboSender.Items.Add(new RadComboBoxItem("", ""));
                foreach (var p in sender)
                {
                    cboSender.Items.Add(new RadComboBoxItem(p.LoginName, p.UserID.ToString()));
                }

                cboSender.SelectedValue = "";



                var assign = gSDMS_BL.mp_User_Lst("3");
                cboAssigned_To. Items.Add(new RadComboBoxItem("", ""));
                foreach (var p in assign)
                {
                    cboAssigned_To.Items.Add(new RadComboBoxItem(p.LoginName, p.UserID.ToString()));
                   
                }

                cboAssigned_To.SelectedValue = "";

        }

        #region event
        private void grdHostngContractDomain_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdProject_Test_List.PageIndex = e.NewPageIndex;
            LoadGrid();
        }
        
        void grdHostngContractDomain_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (ViewState["Direction"] == null) ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " ASC") ViewState["Direction"] = " DESC";
            else if (ViewState["Direction"].ToString() == " DESC") ViewState["Direction"] = " ASC";
            string strSortExp = e.SortExpression + ViewState["Direction"].ToString();
            ViewState["SortExpression"] = strSortExp;
            
            LoadGrid();
        }
       
        void btnSearch_Click(object sender, EventArgs e)
        {
            LoadGrid();
        }
        #endregion
        #region OnInit

        protected override void OnPreInit(EventArgs e)
        {
            AllowSkinOverride = true;
            base.OnPreInit(e);
            Load += Page_Load;
        }

        protected override void OnInit(EventArgs e)
        {
            LoadParams();
            LoadPanels();
            base.OnInit(e);
            grdProject_Test_List.PageIndexChanging += grdHostngContractDomain_PageIndexChanging;

            grdProject_Test_List.Sorting += new GridViewSortEventHandler(grdHostngContractDomain_Sorting); ;
            
            btnSearch.Click += btnSearch_Click;
            btnExport_Excel.Click += new EventHandler(btnExport_Excel_Click);

            //btnFinish.Click += btnFinish_Click;
            SuppressPageMenu();
        }

        void btnExport_Excel_Click(object sender, EventArgs e)
        {
            var s = ViewState["listProjectTest"];
            if (s != null)
            {
                hiddenGrid.DataSource = s;
                hiddenGrid.DataBind();

                var name = ("Project-Tests-" + datFrom_Date.SelectedDate.Value.ToShortDateString() + "-" + datTo_Date.SelectedDate.Value.ToShortDateString()).Replace("/", "").Replace(":", "").Replace(" ", "_");
               
                hiddenGrid.ExportSettings.FileName = name;
                hiddenGrid.ExportSettings.IgnorePaging = true;
                hiddenGrid.ExportSettings.OpenInNewWindow = true;
                hiddenGrid.MasterTableView.ExportToExcel();

                //    ASPxGridViewExporter1.WriteXlsToResponse(name);

            }
        }

        public override void VerifyRenderingInServerForm(Control control)
        {

            // Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time.

        }

        private void LoadParams()
        {
            pageId = WebUtils.ParseInt32FromQueryString("pageid", pageId);
            moduleId = WebUtils.ParseInt32FromQueryString("mid", true, moduleId);
        }

        private void LoadPanels()
        {
            Hashtable settings = ModuleSettings.GetModuleSettings(moduleId);
            //config = new ContactFormConfiguration(settings);
            bool showLeftColumnAdmin = ConfigHelper.GetBoolProperty("ShowLeftColumnAdmin", false);
            bool showRightColumnAdmin = ConfigHelper.GetBoolProperty("ShowRightColumnAdmin", false);
            bool showTopColumnAdmin = ConfigHelper.GetBoolProperty("ShowTopColumnAdmin", false);
            bool showBottomColumnAdmin = ConfigHelper.GetBoolProperty("ShowBottomColumnAdmin", false);
            int showCenterColumnAdmin = ConfigHelper.GetIntProperty("ShowCenterColumnAdmin", 0);
            LoadSideContent(showLeftColumnAdmin, showRightColumnAdmin, showTopColumnAdmin, showBottomColumnAdmin, showCenterColumnAdmin);
        }
        #endregion
    }
}